package com.heima.search.service.Impl;

;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.model.admin.entity.WmSensitive;

import com.heima.model.admin.dto.SensitiveDto;

import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.search.dao.WmSensitiveDao;
import com.heima.search.service.WmSensitiveService;
import org.springframework.stereotype.Service;

import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 自媒体敏感词表(WmSensitive)表服务实现类
 *
 * @author makejava
 * @since 2022-04-22 21:26:19
 */
@Service("wmSensitiveService")
public class WmSensitiveServiceImpl implements WmSensitiveService {
    @Resource
    private WmSensitiveDao wmSensitiveDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmSensitive queryById(Integer id) {
        return this.wmSensitiveDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param wmSensitive 实例对象
     * @return 实例对象
     */
    @Override
    public WmSensitive insert(WmSensitive wmSensitive) {
        this.wmSensitiveDao.insert(wmSensitive);
        return wmSensitive;
    }

    /**
     * 修改数据
     *
     * @param wmSensitive 实例对象
     * @return 实例对象
     */
    @Override
    public WmSensitive update(WmSensitive wmSensitive) {
        this.wmSensitiveDao.update(wmSensitive);
        return this.queryById(wmSensitive.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.wmSensitiveDao.deleteById(id) > 0;
    }


    //分页查询
    @Override
    public ResponseResult queryList(SensitiveDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        PageHelper.startPage(dto.getPage(), dto.getSize());
        Page<WmSensitive> page = wmSensitiveDao.queryAll(dto);
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setData(page.getResult());
        pageResponseResult.setTotal(Integer.valueOf(page.getTotal() + ""));
        pageResponseResult.setSize(page.getPageSize());
        pageResponseResult.setCurrentPage(dto.getPage());
        return pageResponseResult;
    }

    //修改
    @Override
    public ResponseResult updateById(SensitiveDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        WmSensitive wmSensitive = new WmSensitive();
        wmSensitive.setSensitives(dto.getSensitives());
        wmSensitive.setId(Integer.valueOf(dto.getId() + ""));
        wmSensitive.setUpdatedTime(new Date());
        wmSensitiveDao.update(wmSensitive);
        return ResponseResult.okResult("修改成功");
    }
}
